package com.xinling.modules.blog.controller;

import com.xinling.common.api.ApiResult;
import com.xinling.modules.blog.model.dto.PostDTO;
import com.xinling.modules.blog.model.vo.PostVO;
import com.xinling.modules.blog.service.BlogPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "博客文章接口")
@RestController
@RequestMapping("/blog/posts")
public class BlogPostController {

    @Autowired
    private BlogPostService blogPostService;

    @ApiOperation("获取文章详情")
    @GetMapping("/{id}")
    public ApiResult<PostVO> getPost(@PathVariable Integer id) {
        PostVO post = blogPostService.getPostById(id);
        if (post == null) {
            return ApiResult.failed("文章不存在");
        }
        blogPostService.incrementViewCount(id);
        return ApiResult.success(post);
    }

    @ApiOperation("获取所有文章")
    @GetMapping
    public ApiResult<List<PostVO>> getAllPosts(
            @ApiParam("搜索关键词") @RequestParam(required = false) String keyword,
            @ApiParam("分类ID") @RequestParam(required = false) Integer categoryId,
            @ApiParam("标签ID") @RequestParam(required = false) Integer tagId) {
        if (keyword != null && !keyword.isEmpty()) {
            return ApiResult.success(blogPostService.searchPosts(keyword));
        } else if (categoryId != null) {
            return ApiResult.success(blogPostService.getPostsByCategory(categoryId));
        } else if (tagId != null) {
            return ApiResult.success(blogPostService.getPostsByTag(tagId));
        }
        return ApiResult.success(blogPostService.getAllPosts());
    }

    @ApiOperation("获取分类下的文章")
    @GetMapping("/category/{categoryId}")
    public ApiResult<List<PostVO>> getPostsByCategory(@PathVariable Integer categoryId) {
        return ApiResult.success(blogPostService.getPostsByCategory(categoryId));
    }

    @ApiOperation("获取作者的文章")
    @GetMapping("/author/{authorId}")
    public ApiResult<List<PostVO>> getPostsByAuthor(@PathVariable Integer authorId) {
        return ApiResult.success(blogPostService.getPostsByAuthor(authorId));
    }

    @ApiOperation("创建文章")
    @PostMapping
    public ApiResult<PostVO> createPost(@Valid @RequestBody PostDTO postDTO) {
        // TODO: 从SecurityContext中获取当前用户ID
        Integer authorId = 1;
        return ApiResult.success(blogPostService.createPost(postDTO, authorId));
    }

    @ApiOperation("更新文章")
    @PutMapping("/{id}")
    public ApiResult<PostVO> updatePost(@PathVariable Integer id, @Valid @RequestBody PostDTO postDTO) {
        PostVO post = blogPostService.updatePost(id, postDTO);
        if (post == null) {
            return ApiResult.failed("文章不存在");
        }
        return ApiResult.success(post);
    }

    @ApiOperation("删除文章")
    @DeleteMapping("/{id}")
    public ApiResult<Void> deletePost(@PathVariable Integer id) {
        blogPostService.deletePost(id);
        return ApiResult.success();
    }

    @ApiOperation("点赞文章")
    @PostMapping("/{id}/like")
    public ApiResult<Void> likePost(@PathVariable Integer id) {
        blogPostService.incrementLikeCount(id);
        return ApiResult.success();
    }
} 